package com.coffee.websocket.handler;

import com.coffee.websocket.MochaWebsocketException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class HandlerMapping {

    private final static ConcurrentHashMap<Integer, MochaTextHandler> textHandlerMapping = new ConcurrentHashMap<>();
    private final static ConcurrentHashMap<Integer, MochaChatHandler> chatHandlerMapping = new ConcurrentHashMap<>();
    private final static ConcurrentHashMap<Integer, MochaBinaryHandler> binaryHandlerMapping = new ConcurrentHashMap<>();


    private final static ConcurrentHashMap<String, MochaSocketTextHandler> socketHandlerMapping = new ConcurrentHashMap<>();
    private final static ConcurrentHashMap<String, MochaSocketTextJsonHandler> socketJsonHandlerMapping = new ConcurrentHashMap<>();

    @Getter
    private final static WebsocketHandlerParameter handlerParameter = new WebsocketHandlerParameter();


    //------------get--------------------------
    public static MochaTextHandler getTextHandler(Integer cmd) throws MochaWebsocketException {
        if (textHandlerMapping.isEmpty()){
            throw new MochaWebsocketException("Initialize MochaCmdHandler analysis handler error!");
        }
        return textHandlerMapping.get(cmd);
    }

    public static MochaChatHandler getChatHandler(Integer cmd) throws MochaWebsocketException {
        if (chatHandlerMapping.isEmpty()){
            throw new MochaWebsocketException("Initialize MochaChatHandler analysis handler error!");
        }
        return chatHandlerMapping.get(cmd);
    }

    public static MochaBinaryHandler getBinaryHandler(Integer cmd) throws MochaWebsocketException {
        if (binaryHandlerMapping.isEmpty()){
            throw new MochaWebsocketException("Initialize MochaChatHandler analysis handler error!");
        }
        return binaryHandlerMapping.get(cmd);
    }

    public static MochaSocketTextHandler getSocketHandler(String event){
        if (socketHandlerMapping.isEmpty()){
            throw new MochaWebsocketException("Initialize textCommandHandlerMapping analysis handler error!");
        }
        return socketHandlerMapping.get(event);
    }

    public static MochaSocketTextJsonHandler getMochaSocketTextJsonHandler(String event){
        if (socketJsonHandlerMapping.isEmpty()){
            throw new MochaWebsocketException("Initialize SocketJsonHandlerMapping analysis handler error!");
        }
        return socketJsonHandlerMapping.get(event);
    }



    public static MochaConnectHandler getConnectHandler(){
        return handlerParameter.getConnectHandler();
    }

    public static MochaCloseHandler getCloseHandler(){
        return handlerParameter.getCloseHandler();
    }

    public static MochaUserValidHandler getUserValidHandler(){
        return handlerParameter.getUserValidHandler();
    }

    public static MochaSocketConnectHandler getSocketConnectHandler(){
        return handlerParameter.getSocketConnectHandler();
    }

    public static MochaSocketCloseHandler getSocketCloseHandler(){
        return handlerParameter.getSocketCloseHandler();
    }
    public static MochaSocketUserValidHandler getSocketUserValidHandler(){
        return handlerParameter.getSocketUserValidHandler();
    }





    //---------------------------------initialize------------------------

    public static void initializeConnectHandler(MochaConnectHandler connectHandler){
        handlerParameter.setConnectHandler(connectHandler);
    }

    public static void initializeCloseHandler(MochaCloseHandler closeHandler){
        handlerParameter.setCloseHandler(closeHandler);
    }

    public static void initializeUserValidHandler(MochaUserValidHandler userValidHandler){
        handlerParameter.setUserValidHandler(userValidHandler);
    }
    public static void initializeSocketConnectHandler(MochaSocketConnectHandler socketConnectHandler){
        handlerParameter.setSocketConnectHandler(socketConnectHandler);
    }
    public static void initializeSocketClosetHandler(MochaSocketCloseHandler socketCloseHandler){
        handlerParameter.setSocketCloseHandler(socketCloseHandler);
    }
    public static void initializeSocketUserValidHandler(MochaSocketUserValidHandler socketUserValidHandler){
        handlerParameter.setSocketUserValidHandler(socketUserValidHandler);
    }


    public static void initializeTextHandler(Map<Integer, MochaTextHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaTextHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        textHandlerMapping.putAll(handlerMap);
    }

    public static void initializeBinaryHandler(Map<Integer, MochaBinaryHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaBinaryHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        binaryHandlerMapping.putAll(handlerMap);
    }

    public static void initializeChatHandler(Map<Integer, MochaChatHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaChatHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        chatHandlerMapping.putAll(handlerMap);
    }

    public static void initializeSocketTextHandler(Map<String, MochaSocketTextHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaChatHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        socketHandlerMapping.putAll(handlerMap);
    }
    public static void initializeSocketTextJsonHandler(Map<String, MochaSocketTextJsonHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           return;
       }
        socketJsonHandlerMapping.putAll(handlerMap);
    }

}
